package com.jcl.recruit.elasticsearch.thread;

import com.jcl.core.utils.HttpRequest;
import com.jcl.recruit.elasticsearch.core.utils.CrawlerConfigUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @ClassName ResumeDownloadTask
 * @Author yyliu
 * @Date 2018/12/25 17:25
 * @Version 1.0
 **/
public class ResumeDownloadTask {

    private static String url = CrawlerConfigUtils.downloadUrl();

    public static void start(Map<String, Object> context) {
        ResumeDownloadThread resumeDownloadRunnable = new ResumeDownloadThread(context);
        DownloadThread thread = new DownloadThread(resumeDownloadRunnable);
        thread.start();
    }

    public static class DownloadThread extends Thread {

        private ResumeDownloadThread resumeDownloadRunnable;

        public DownloadThread(ResumeDownloadThread resumeDownloadRunnable) {
            this.resumeDownloadRunnable = resumeDownloadRunnable;
        }

        public ResumeDownloadThread getResumeDownloadRunnable() {
            return resumeDownloadRunnable;
        }

        public void setResumeDownloadRunnable(ResumeDownloadThread resumeDownloadRunnable) {
            this.resumeDownloadRunnable = resumeDownloadRunnable;
        }

        @Override
        public synchronized void start() {
            resumeDownloadRunnable.run();
        }
    }

    public static class ResumeDownloadThread implements Runnable {

        private static final Logger logger = LoggerFactory.getLogger(ResumeDownloadThread.class);

        @Override
        public void run() {
            logger.info("download start ...");
            handle();
            logger.info("download end . ");
        }

        private void handle() {
            logger.info("download params : {}", this.context);
            HttpRequest.httpPost(url, HttpRequest.JSON_CONTENT_TYPE, this.context);
        }

        public ResumeDownloadThread(Map<String, Object> context) {
            this.context = context;
        }

        public Map<String, Object> getContext() {
            return context;
        }

        public void setContext(Map<String, Object> context) {
            this.context = context;
        }

        private Map<String, Object> context;
    }

}
